<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true"  v-show="showSearch" label-width="80px">
          <el-form-item label="帮助标题" prop="infoTitle">
            <el-input v-model="queryParams.infoTitle" placeholder="请输入帮助标题" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="所属分类" prop="categoryName">
            <el-input v-model="queryParams.categoryName" placeholder="请输入所属分类" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="帮助简介" prop="introduction">
            <el-input v-model="queryParams.introduction" placeholder="请输入帮助简介" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="帮助内容" prop="infoContent">
            <el-input v-model="queryParams.infoContent" placeholder="请输入帮助内容" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary"  v-hasPermi="['saveHelpInfo']"  icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" v-hasPermi="['updateHelpInfo']"  icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" v-hasPermi="['putRecycleHelpInfo']"  icon="el-icon-milk-tea" size="mini" :disabled="single" @click="handlePutRecycle">回收站</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="queryMyHelpInfoPageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table ref="multipleTable" :stripe="true" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
                    @sort-change='tableSortChange' :default-sort="{prop: 'sort', order: 'descending'}">
          <el-table-column type="selection" align="center"/>
          <el-table-column label="帮助标题" align="left" prop="infoTitle"/>
          <el-table-column label="帮助简介" align="left" prop="introduction" width="350"  :show-overflow-tooltip="true"/>
          <el-table-column label="所属分类" align="left" prop="categoryName" width="150"/>
          <el-table-column label="创建时间" align="center" prop="createTime" sortable='custom' width="200" />
          <el-table-column label="更新时间" align="center" prop="updateTime" sortable='custom' width="200" />
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" v-hasPermi="['updateHelpInfo']" icon="el-icon-edit"  @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini" type="text" v-hasPermi="['putRecycleHelpInfo']" icon="el-icon-milk-tea"  @click="handlePutRecycle(scope.row)">回收站</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="queryMyHelpInfoPageResList"/>
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="addUpdateOpenRules"  label-width="80px">
        <el-form-item label="帮助标题" prop="infoTitle">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.infoTitle" placeholder="请输入帮助标题" maxlength="250" />
        </el-form-item>

        <el-form-item label="所属分类" prop="categoryId">
          <treeselect size="small" clearable style="width: 100%"
                      v-model="form.categoryId"
                      :options="helpCategoryOptions"
                      :defaultExpandLevel="2"
                      :normalizer="normalizerTree"
                      :show-count="true"
                      placeholder="请选择所属分类" />
        </el-form-item>

        <el-form-item label="帮助简介" prop="introduction">
          <el-input type="textarea" autocomplete="off" :rows="2"  v-model="form.introduction" placeholder="请输入帮助简介" maxlength="150" />
        </el-form-item>

        <el-form-item label="帮助内容" prop="infoContent">
          <vue-ueditor-wrap v-model="form.infoContent" :config="myConfig"></vue-ueditor-wrap>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number :hide-required-asterisk="true" v-model="form.sort" :min="1" :max="10000" label="排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelAddUpdateOpen">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { baiDuUeditor } from '@/utils/ueditorConfig'
  import VueUeditorWrap from "vue-ueditor-wrap"; // ES6 Module
  import {querySelectHelpCategoryTreeResList} from '@/api/help/helpCategory'
  import {queryMyHelpInfoPageResList, saveHelpInfo, updateHelpInfo, putRecycleHelpInfoByInfoId} from '@/api/help/helpInfo'
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  //默认查询参数
  let defaultQueryParams = {
    infoContent: undefined,
    introduction: undefined,
    infoTitle: undefined,
    categoryId: undefined,
    categoryName: undefined,
    page: 1,
    rows: 10
  }
  export default {
    name: 'myHelpInfoAdmin',
    components: {
      VueUeditorWrap,
      Treeselect
    },
    data() {
      return {
        // ueditor
        myConfig: baiDuUeditor(350),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作帮助信息发布
        dataList: [],
        //分类选项
        helpCategoryOptions: [],
        // 日期范围
        dateRange: [],
        // 弹出层标题
        title: "",
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        // 增加修改表单参数
        form: {},
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列帮助信息
        columns: [],
        // 新增修改表单校验
        addUpdateOpenRules: {
          categoryId: [
            { required: true, message: "所属分类必须选择"},
          ],
          infoTitle: [
            { required: true, message: "帮助标题不能为空"},
          ],
          introduction: [
            { required: true, message: "帮助简介不能为空"},
          ],
          infoContent: [
            { required: true, message: "帮助内容不能为空"},
          ],
          sort: [
            { required: true, message: "排序不能为空",trigger: "blur" },
          ]
        },
      }
    },
    created() {
      //加载帮助信息分类
      this.querySelectHelpCategoryTreeResList()
      //查询帮助信息发布列表
      this.queryMyHelpInfoPageResList()
    },
    methods: {
      /** 转换分类数据结构 */
      normalizerTree(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.categoryId,
          label: node.categoryName,
          children: node.children
        };
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.infoId)
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.queryMyHelpInfoPageResList()
      },

      /** 帮助信息分类查询 */
      querySelectHelpCategoryTreeResList() {
        querySelectHelpCategoryTreeResList({}).then(response => {
          this.helpCategoryOptions = response.data;
        });
      },

      /** 查询帮助信息发布列表 */
      queryMyHelpInfoPageResList() {
        this.loading = true
        queryMyHelpInfoPageResList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        //加载帮助信息分类
        this.querySelectHelpCategoryTreeResList()
        this.addUpdateOpen = true;
        this.title = "添加帮助信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        //修改表单填充
        this.form = {...row};
        //加载帮助信息分类
        this.querySelectHelpCategoryTreeResList()
        this.addUpdateOpen = true;
        this.title = "修改帮助信息";
      },
      /** 放入回收站按钮操作 */
      handlePutRecycle(row) {
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        this.$confirm('是否确认把所选帮助信息放入回收站?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const infoId={infoId:row.infoId}
          return putRecycleHelpInfoByInfoId(infoId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.queryMyHelpInfoPageResList();
        }).catch(function() {});
      },

      /** 提交保存或修改按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.infoId !== undefined) {
              updateHelpInfo(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.queryMyHelpInfoPageResList();
                this.addUpdateOpen = false
              });
            } else {
              saveHelpInfo(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.queryMyHelpInfoPageResList();
                this.addUpdateOpen = false
              });
            }
          }
        });
      },

      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = []
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.queryMyHelpInfoPageResList()
      },
      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },
      // 表单重置
      reset() {
        this.form = {
          infoId: undefined,
          categoryId: undefined,
          infoTitle: undefined,
          introduction: undefined,
          infoContent: undefined,
          sort: 1
        };
        this.resetForm('form')
      }
    }
  }
</script>
